<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.7.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="今天来看一下Android Studio中的多渠道打包功能。主要应用到了Gradle中的Product Flavors配置。">
<meta name="keywords" content="android">
<meta property="og:type" content="article">
<meta property="og:title" content="Android Studio中的多渠道包开发">
<meta property="og:url" content="https://ricky9090.github.io/2016/07/05/android_flavor_apk/index.html">
<meta property="og:site_name" content="Ricky&#39;s Blog">
<meta property="og:description" content="今天来看一下Android Studio中的多渠道打包功能。主要应用到了Gradle中的Product Flavors配置。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://img.blog.csdn.net/20160705144907840">
<meta property="og:image" content="http://img.blog.csdn.net/20160705144928355">
<meta property="og:image" content="http://img.blog.csdn.net/20160705145022402">
<meta property="og:image" content="http://img.blog.csdn.net/20160705145145341">
<meta property="og:image" content="http://img.blog.csdn.net/20160705145330419">
<meta property="og:image" content="http://img.blog.csdn.net/20160705145502670">
<meta property="og:updated_time" content="2018-06-11T08:46:43.914Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android Studio中的多渠道包开发">
<meta name="twitter:description" content="今天来看一下Android Studio中的多渠道打包功能。主要应用到了Gradle中的Product Flavors配置。">
<meta name="twitter:image" content="http://img.blog.csdn.net/20160705144907840">
  <link rel="canonical" href="https://ricky9090.github.io/2016/07/05/android_flavor_apk/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>Android Studio中的多渠道包开发 | Ricky's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Ricky's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">A long way to go.</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-about">
      
    

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://ricky9090.github.io/2016/07/05/android_flavor_apk/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Ricky">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Ricky's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">Android Studio中的多渠道包开发

          
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2016-07-05 15:00:16" itemprop="dateCreated datePublished" datetime="2016-07-05T15:00:16+08:00">2016-07-05</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2018-06-11 16:46:43" itemprop="dateModified" datetime="2018-06-11T16:46:43+08:00">2018-06-11</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>今天来看一下Android Studio中的多渠道打包功能。主要应用到了Gradle中的Product Flavors配置。</p>
<a id="more"></a>
<p>首先建立一个普通的APP工程。创建好后默认的结构如下：<br><br><img src="http://img.blog.csdn.net/20160705144907840" alt=""><br><br><br>可以看到默认包含main这一主干版本。<br>接下来加入我们向发布一个Foo的渠道版本，需要对工程进行配置。<br>结合网上查到的资料和我的实践，<strong>简要来说有以下几个注意点</strong>：</p>
<p>1.最新的Android Studio已经可以通过图形界面来配置多个Flavor。<br><br>2.若不使用图形界面配置Flavor，那么需要手工编写app的build.gradle文件<br><br>3.如果不预先配置Flavor信息，那么在配置工程目录及源码文件时，不会有相应的辅助功能。</p>
<p>因此我们按照顺序：<br>先配置Flavor信息，然后再配置Flavor的源码及相关文件目录。<br>图形界面配置：<br>1.首先右键点击工程目录，选择“open module settings”<br>2.打开app的flavor选项卡，按照需要填写相关配置。如图：<br><br><img src="http://img.blog.csdn.net/20160705144928355" alt=""><br><br><br>打开app工程的build.gradle脚本，发现Android Studio已经帮我们自动填充了Product Flavors的配置。<br>因此，若想手动编写gradle代码，直接写入这些配置即可：<br><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">productFlavors &#123;</span><br><span class="line">    foo &#123;</span><br><span class="line">        minSdkVersion <span class="number">15</span></span><br><span class="line">        applicationId <span class="string">'com.example.multiflavorfoo'</span></span><br><span class="line">        targetSdkVersion <span class="number">23</span></span><br><span class="line">        versionCode <span class="number">1</span></span><br><span class="line">        versionName <span class="string">'1.0'</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>接下来创建Flavor相应的目录。<br>1.右键单击app文件夹，新建目录并命名为foo。<br>2.在foo中继续新建一个文件夹java。<br><br><img src="http://img.blog.csdn.net/20160705145022402" alt=""><br><br><br>可以看到Android Studio默认将java文件夹标示成蓝色图标（与main的java相同，表示这是一个源码文件夹），此时右键点击java文件夹，可以新建package。若是普通的黄色标识文件夹，右键菜单是没有新建package选项的。</p>
<p>我们也可以在gradle中指定某个flavor的路径：<br><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">sourceSets</span> &#123;</span><br><span class="line">    foo &#123;</span><br><span class="line">        java.srcDirs = [<span class="string">'src/foo/java'</span>]</span><br><span class="line">        res.srcDirs = [<span class="string">'src/foo/res'</span>]</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>继而在此新建package并创建MainActivity.java。<br>这时可以看到Android Studio报错，说此处有类冲突(Duplicate class)——MainActivity和main主干中的MainActivity冲突了。<br><br><img src="http://img.blog.csdn.net/20160705145145341" alt=""><br><br><br>这是由于gradle编译flavor时，总会检查分支代码路径和主干代码路径，所以不可避免的产生了类冲突。<br>假如确实有这种需求：两个分支中存在某个相同名称的类。那么可以再新建一个分支bar，使用foo和bar开发，并将main中的重名类删除掉。</p>
<p>仿照刚才的步骤，新建一个bar分支：<br><figure class="highlight gradle"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">productFlavors &#123;</span><br><span class="line">        foo &#123;</span><br><span class="line">            minSdkVersion <span class="number">15</span></span><br><span class="line">            applicationId <span class="string">'com.example.multiflavorfoo'</span></span><br><span class="line">            targetSdkVersion <span class="number">23</span></span><br><span class="line">            versionCode <span class="number">1</span></span><br><span class="line">            versionName <span class="string">'1.0'</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        bar &#123;</span><br><span class="line">            minSdkVersion <span class="number">15</span></span><br><span class="line">            applicationId <span class="string">'com.example.multiflavorbar'</span></span><br><span class="line">            targetSdkVersion <span class="number">23</span></span><br><span class="line">            versionCode <span class="number">1</span></span><br><span class="line">            versionName <span class="string">'1.0'</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p><br></p>
<p>然后再新建相应文件夹，会发现java文件夹不会默认标蓝，也不能在下面创建package了。<br><br><img src="http://img.blog.csdn.net/20160705145330419" alt=""><br><br><br>实际在windows资源管理器中手动创建这些文件夹是完全没有问题的，而且运行gradle命令也可以正确编译工程。<br>这里实际是Android Studio上层给开发人员的限制——同一时间仅可在一个flavor上开发。</p>
<p>如果想切换到bar flavor开发，需要在Build Variants面板中选择bar为当前的Build Variant。<br>然后就会发现foo这一分支不可操作了，bar下的文件夹按照我们的配置可以正常使用了。<br><br><img src="http://img.blog.csdn.net/20160705145502670" alt=""><br><br><br>由于foo分支不是当前编译选定的分支，因此java中的package文件夹都按普通的文件夹处理显示。</p>
<p>这时将main中的MainActivity剪切到bar下，会发现即使切换回foo分支，类冲突的错误也不存在了。</p>
<p>开发完不同flavor，可使用<code>gradle task</code>命令查看各个分支可编译的选项，运行相应命令；<br>或者通过Build Variant面板选择当前要编译的版本，在Android Studio中运行即可。</p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/android/" rel="tag"># android</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2016/07/04/android_output_jar/" rel="next" title="Android Studio导出jar包">
                  <i class="fa fa-chevron-left"></i> Android Studio导出jar包
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2016/07/17/docker_note/" rel="prev" title="Docker学习笔记">
                  Docker学习笔记 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Ricky</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/ricky9090" title="GitHub &rarr; https://github.com/ricky9090" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ricky</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.7.1</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.4.0</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  





















  

  

  

</body>
</html>
